﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace AcmeSportsStore
{
    public static class Password
    {
        /*
         *   Encode the password
         */
        private static string Encode(string password)
        {
            char[] chr = new char[password.Length];
            for (int i = 0; i < password.Length; i++)
            {
                chr[i] = Convert.ToChar(Convert.ToInt32(password[i]) + 1);
            }
            return (new string(chr));
        }
        /*
         * Decode the password
         */
        private static string Decode(string password)
        {
            char[] chr = new char[password.Length];
            for (int i = 0; i < password.Length; i++)
            {
                chr[i] = Convert.ToChar(Convert.ToInt32(password[i]) - 1);
            }
            return (new string(chr));
        }

        public static bool CompPassword(string getPassword)
        {
            StreamReader sr = new StreamReader("E:\\Password.txt");
            string passwordInFile = sr.ReadLine();
            sr.Close();
            //Convert.ToInt32(line) - 1) / 2 is decode
            if (Decode(passwordInFile).Equals(getPassword))
                return true;
            else
                return false;
        }

        public static bool NewPassword(string newCode1, string newCode2)
        {
            if (newCode1.Equals(newCode2))
            {
                StreamWriter sw = new StreamWriter("E:\\Password.txt");
                sw.WriteLine(Encode(newCode1));
                sw.Close();
                return true;
            }
            else
                return false;
        }
    }
}
